﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Controls;

namespace TestShit
{
    /// <summary>
    /// Interaction logic for Car.xaml
    /// </summary>
    public partial class Car : UserControl
    {
        public Car()
        {
            InitializeComponent();

            _animX = new DoubleAnimationUsingPath();
            _animX.CurrentTimeInvalidated += new EventHandler(animXTick);
            _animX.Source = PathAnimationSource.X;
            _animY = new DoubleAnimationUsingPath();
            _animY.Source = PathAnimationSource.Y;
            _animAngle = new DoubleAnimationUsingPath();
            _animAngle.Source = PathAnimationSource.Angle;

            _story = new Storyboard();
            _story.Children.Add(_animAngle);
            _story.Children.Add(_animY);
            _story.Children.Add(_animX);

            _animCompleted = false;
        }
        private bool _animCompleted;
        //private Point relativePoint;
        private AnimationClock _clockX;
        private AnimationClock _clockY;
        private AnimationClock _clockAngle;
        private Storyboard _story;
        public AnimationClock ClockX
        {
            get
            {
                return _clockX;
            }            
        }
        private DoubleAnimationUsingPath _animX;
        private DoubleAnimationUsingPath _animY;
        private DoubleAnimationUsingPath _animAngle;
        public PathGeometry AnimationPath
        {
            get
            {
                return _animX.PathGeometry;
            }
            set
            {
                _animX.PathGeometry = value;
                _animY.PathGeometry = value;
                _animAngle.PathGeometry = value;
            }
        }
        public int AnimationDuration
        {
            set
            {
                _animX.Duration = TimeSpan.FromSeconds(value);
                _animY.Duration = TimeSpan.FromSeconds(value);
                _animAngle.Duration = TimeSpan.FromSeconds(value);
            }
        }

        public event EventHandler AnimationCompleted;

        public void OpenAnimation()
        {
            
            _clockX = _animX.CreateClock();
            CarTranslateTransform.ApplyAnimationClock(TranslateTransform.XProperty, _clockX);
            _clockX.Controller.Stop();
            _clockY = _animY.CreateClock();
            CarTranslateTransform.ApplyAnimationClock(TranslateTransform.YProperty, _clockY);
            _clockY.Controller.Stop();
            _clockAngle = _animAngle.CreateClock();
            CarRotateTransform.ApplyAnimationClock(RotateTransform.AngleProperty, _clockAngle);
            _clockAngle.Controller.Stop();
            _clockAngle.Completed += new EventHandler(Animation_Completed);
        }

        private void Animation_Completed(object sender, EventArgs e)
        {
            double x = 0, y = 0;
            if (_clockX != null)
            {
                x = _animX.GetCurrentValue(0, 0, _clockX);
            }
            if (_clockY != null)
            {
                y = _animY.GetCurrentValue(0, 0, _clockY);
            }
            //relativePoint.X += x;
            //relativePoint.Y += y;
            _animCompleted = true;
            if (AnimationCompleted != null)
            {
                AnimationCompleted(this, e);
            }
            _animCompleted = false;
        }

        public void StartAnimation()
        {
            
            _clockX.Controller.Begin();
            _clockY.Controller.Begin();
            _clockAngle.Controller.Begin();
        }

        public void StopAnimation()
        {
            _clockX.Controller.Stop();
            _clockY.Controller.Stop();
            _clockAngle.Controller.Stop();     
        }

        public void ContinueAnimation()
        {
            _clockX.Controller.Resume();
            _clockY.Controller.Resume();
            _clockAngle.Controller.Resume();
        }

        public void PauseAnimation()
        {
            _clockX.Controller.Pause();
            _clockY.Controller.Pause();
            _clockAngle.Controller.Pause();
        }

        public bool IsAnimationPaused()
        {
            return _clockAngle.IsPaused;
        }

        public bool Collide(Car otherCar)
        {
            // Ütközés vizsgálat
            double thisX = 0, thisY = 0;
            double otherX = 0, otherY = 0;
            if (_animCompleted)
            {
                return true;
            }

            if (_clockX != null) 
            {
                thisX = _animX.GetCurrentValue(0, 0, _clockX);
            }
            if (_clockY != null)
            {
                thisY = _animY.GetCurrentValue(0, 0, _clockY);
            }
            if (otherCar._clockX != null)
            {
                otherX = otherCar._animX.GetCurrentValue(0, 0, otherCar._clockX);
            }
            if (otherCar._clockY != null)
            {
                otherY = otherCar._animY.GetCurrentValue(0, 0, otherCar._clockY);
            }
           // return IsPositionsCollide(relativePoint.X + thisX, relativePoint.Y + thisY,
            //  otherCar.relativePoint.X + otherX, otherCar.relativePoint.Y + otherY);
            return IsPositionsCollide(thisX, thisY, otherX, otherY);
        }

        private bool IsPositionsCollide(double x1, double y1, double x2, double y2)
        {
            double dist = Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
            // Ha a megadott két pont távolsága kisebb mint valamennyi, akkor ütközésnek számít
            // Itt most a befoglaló dobozok alapján történik a minimális távolságszámítás, de élesben lehet jobb kell majd
            return dist <= Math.Sqrt(((CarBox.ActualWidth*CarBox.ActualWidth)* 2));
        }
        private void RefreshLabel()
        {
            double realX = 0, realY = 0;
            if (_clockX != null)
            {
                realX = _animX.GetCurrentValue(0, 0, _clockX);
            }
            if (_clockY != null)
            {
                realY = _animY.GetCurrentValue(0, 0, _clockY);
            }
            //realX += relativePoint.X;
            //realY += relativePoint.Y;
            CarLabel.Content = "(" + Convert.ToInt32(realX).ToString() + "," + Convert.ToInt32(realY).ToString() + ")";
        }

        private void UserControl_Loaded_1(object sender, System.Windows.RoutedEventArgs e)
        {
            //relativePoint = this.TransformToAncestor((Visual)this.Parent).Transform(new Point(0, 0));
            RefreshLabel();
        }

        private void animXTick(object sender2, EventArgs e2)
        {
            RefreshLabel();
        }
    }
}
